import scrapy
import json
from urllib.parse import urlencode
from scrapy.http.request.json_request import JsonRequest


class RequestMethodSpider(scrapy.Spider):
    name = "request_method"

    async def start(self):
        # yield scrapy.Request(f'http://httpbin.org/get?name=zdz&{json.dumps('age=10&add=101')}',method='GET')

        # yield scrapy.Request(f'http://httpbin.org/post?name=zdz',method='POST',headers={
        #     'Content-Type': 'application/x-www-form-urlencoded',
        # },body=urlencode({
        #     'age':'10',
        #     'add':'1111'
        # }))

        # yield scrapy.Request('http://httpbin.org/post?name=zdz', method='POST', headers={
        #     'Content-Type': 'application/json',
        # }, body=json.dumps({
        #     'age': 10,
        #     'add': 101
        # }))

        # yield scrapy.FormRequest('http://httpbin.org/post?name=zdz', formdata={
        #     'age': '20',
        #     'add': '101'
        # })

        yield JsonRequest('http://httpbin.org/post?name=zdz', data={
            'age': 10,
            'add': 101
        })

    def parse(self, response):
        print(response.json())
